package com.appwx.sdk.http;


import java.io.IOException;

import com.appwx.sdk.model.AppParameters;
import com.appwx.sdk.model.OpenApiModel;
import com.appwx.sdk.view.WeiboException;

import android.content.Context;
import android.os.AsyncTask;

public class AsyncAppRunner {

	private OpenApiModel mOpenApiModel;

	public AsyncAppRunner(OpenApiModel openApiModel) {
		this.mOpenApiModel = openApiModel;
	}

	public void request(final Context context, final String url,
			final AppParameters parameters, final String httpMethod,
			final RequestListener listener) {
		new AsyncTask<String, Integer, String>() {

			@Override
			protected String doInBackground(String... params) {
				String resp = null;
				try {
					resp = mOpenApiModel.request(context, url, parameters,httpMethod, mOpenApiModel.getAccessToken());
				} catch (WeiboException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				return resp;
			}
			protected void onPostExecute(String result) {
				listener.onComplete(result);
			};
		}.execute("");

	}

	public static interface RequestListener {

		public void onComplete(String response);

		public void onIOException(IOException e);

		public void onError(WeiboException e);

	}

}
